package com.thalesgroup.preferenceFramework.models.LCPnet;

import net.sourceforge.jFuzzyLogic.FIS;
import net.sourceforge.jFuzzyLogic.rule.FuzzyRuleSet;

import org.eclipse.emf.common.util.EList;

import com.thalesgroup.preferenceFramework.models.Node;
import com.thalesgroup.preferenceFramework.models.LCPnet.exceptions.LCPnetEvaluationException;
import com.thalesgroup.preferenceFramework.models.LCPnet.impl.LCPnetImpl;

public class Manual_jFuzzyLogic_Test {

	public static void main(String[] args) throws LCPnetEvaluationException {

		LCPnet net = LCPnetImpl
				.load("tests/com/thalesgroup/preferenceFramework/models/LCPnet/impl/lcpnet_test.lcpnet");
		if (net == null) {
			throw new LCPnetEvaluationException(
					"Error while loading lcpnet file for evaluation");
		}

		EList<Node> nodes = net.getNodes();
		for (Node node : nodes) {
			if (node.getName().equals("R")) {
				LNode lnode = (LNode) node;
				FIS nodeFIS = lnode.getFuzzyInferenceSystem();
				FuzzyRuleSet fuzzyRuleSet = nodeFIS.getFuzzyRuleSet();
				fuzzyRuleSet.chart();

				// Print ruleSet
				System.out.println(fuzzyRuleSet.toStringFCL());

				// Set inputs
				fuzzyRuleSet.setVariable("B", 1);
				fuzzyRuleSet.setVariable("R", 0.6);
				// Evaluate fuzzy set
				fuzzyRuleSet.evaluate();

				// Show output variable's chart
				fuzzyRuleSet.getVariable("Utility").chartDefuzzifier(true);

				// Print ruleSet
				System.out.println(fuzzyRuleSet);

				// Print R utility value
				System.out.println("R utility value = "
						+ fuzzyRuleSet.getVariable("Utility")
								.getLatestDefuzzifiedValue());
			}
		}

	}
}
